package mapper

import (
	"fmt"
	"gorm.io/gorm"
	"ylb/config"
	"ylb/entity"
	"ylb/tool"
)

type UserMapper struct {
	DB *gorm.DB
}

var url tool.Url

func NewUserMapper() UserMapper {
	url = config.GetDBUrlConfig()
	userMapper := UserMapper{}
	userMapper.DB = tool.GetGormConnection(url)
	return userMapper
}

func (userMapper *UserMapper) SelectUserById(id uint) entity.User {
	user := entity.User{}
	tx := userMapper.DB.Where("id = ?", id).First(&user)

	err := tx.Error
	if err != nil {
		fmt.Println("SelectUserById 出错")
		fmt.Println(err)

	}
	return user
}

func (userMapper *UserMapper) SelectUserByUserName(userName string) entity.User {
	user := entity.User{}
	tx := userMapper.DB.Where("user_name = ?", userName).First(&user)
	err := tx.Error
	if err != nil {
		err.Error()
	}

	return user
}
func (userMapper *UserMapper) Update(user entity.User) int64 {

	return userMapper.DB.Updates(&user).RowsAffected
}

func (userMapper *UserMapper) InsertOne(user *entity.User) int64 {

	return userMapper.DB.Create(&user).RowsAffected
}
func (userMapper *UserMapper) SelectUserRelevance(id uint) entity.UserRelevance {
	relevance := entity.UserRelevance{}
	userMapper.DB.Where("user_id = ?", id).First(&relevance)
	return relevance
}
